package com.my.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * NIO channel1
 * @author Lelezhang
 * @create 2020-07-26 23:21
 */
public class NIOFileChannel1 {
    public static void main(String[] args) throws Exception {
        // 指定文件位置
        File file = new File("channel1.txt");
        // 获取输入流
        FileInputStream fileInputStream = new FileInputStream(file);
        // 获取channel
        FileChannel channel = fileInputStream.getChannel();
        // 创建字节缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());
        // 将缓冲区文件内容读取到缓冲区
        channel.read(byteBuffer);
        // 输出缓冲区内容
        System.out.println(new String(byteBuffer.array()));
        fileInputStream.close();
    }
}
